package common.impl;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RequestWrapper extends HttpServletRequestWrapper {
	private HttpServletResponse response = null;

	public RequestWrapper(HttpServletRequest request) {
		super(request);
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		HttpSession session = super.getSession();
		processSessionCookie(session);
		return session;
	}

	public HttpSession getSession(boolean create) {
		HttpSession session = super.getSession(create);
		processSessionCookie(session);
		return session;
	}

	private void processSessionCookie(HttpSession session) {
		if (null == response || null == session) {
			return;
		}

		// cookieOverWritten - 用于过滤多个Set-Cookie头的标志
		Object cookieOverWritten = getAttribute("COOKIE_OVERWRITTEN_FLAG");
		 //session.isNew() && 		
		if(isSecure()) {
			// 当是https协议，且新session时，创建JSESSIONID cookie以欺骗浏览器

			Cookie cookie = new Cookie("JSESSIONID", session.getId());
			cookie.setMaxAge(-1); //有效时间为浏览器打开或超时
			String contextPath = getContextPath();
			//String path = this.getServerName() + this.getServerPort();
			if ((contextPath != null) && (contextPath.length() > 0)) {
				cookie.setPath(contextPath);
			} else {
				cookie.setPath("/");
			}
			//cookie.setPath("/");
			response.addCookie(cookie); //增加一个Set-Cookie头到response
		}
	}

}